// 根据文件扩展名获取 Content-Type
function getContentType(filePath) {
  if (filePath.endsWith('.png')) {
    return 'image/png';
  } else if (filePath.endsWith('.jpg') || filePath.endsWith('.jpeg')) {
    return 'image/jpeg';
  } else if (filePath.endsWith('.gif')) {
    return 'image/gif';
  }
  return 'application/octet-stream';
}

// 将可读流转换为 Buffer
function streamToBuffer(stream) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    stream.on('data', (chunk) => {
      chunks.push(chunk);
    });
    stream.on('end', () => {
      resolve(Buffer.concat(chunks));
    });
    stream.on('error', (error) => {
      reject(error);
    });
  });
}

module.exports = {
  getContentType,
  streamToBuffer
}